package com.elderly.utils;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;

import javax.annotation.Resource;

/**
 * @Description
 * @Date 2021/10/5 16:46
 * @Created 30500
 */
@Component
public class RedisUtils {
    /**
     * 注入连接池
     */
    @Resource
    private JedisPool jedisPool;

    /**
     * 注入redis连接对象
     */
    @Resource
    private Jedis jedis;

    /**
     * @description 保存普通数据
     * @author 30500
     * @date 2021/10/6 16:00
     * @type [java.lang.String 键,
     *        java.lang.String 值,
     *        int 失效时间]
     * @return boolean
     */
    public boolean set(String key, String value, int expireSecond) {
        Transaction multi = jedis.multi();
        try {
            // Jedis jedis = jedisPool.getResource();
            multi.set(key, value, "NX", "EX", expireSecond);
            multi.exec();
        } catch (Exception e) {
            e.printStackTrace();
            multi.discard();
            return false;
        }
        return true;
    }

    /**
     * @description 存入键值对
     * @author 30500
     * @date 2021/10/7 9:12
     * @type [java.lang.String 键,
     *        java.lang.String 值]
     * @return boolean
     */
    public boolean set(String key, String value) {
        Transaction multi = jedis.multi();
        try {
            // Jedis jedis = jedisPool.getResource();
            multi.set(key, value);
            multi.exec();
        } catch (Exception e) {
            e.printStackTrace();
            multi.discard();
            return false;
        }
        return true;
    }

    /**
     * @description 递增
     * @author 30500
     * @date 2021/10/7 10:24
     * @type [java.lang.String 键]
     * @return boolean
     */
    public boolean increase(String key) {
        Transaction multi = jedis.multi();
        try {
            // Jedis jedis = jedisPool.getResource();
            multi.incr(key);
            multi.exec();
        } catch (Exception e) {
            e.printStackTrace();
            multi.discard();
            return false;
        }
        return true;
    }
    /**
     * @description 存储hash数据
     * @author 30500
     * @date 2021/10/6 16:02
     * @type [java.lang.String 键,
     *        java.lang.String 字段,
     *        java.lang.String 值,
     *        int 失效时间]
     * @return boolean
     */
    public boolean set(String key, String field, String value, int time) {
        return true;
    }

    /**
     * @description 取出数据
     * @author 30500
     * @date 2021/10/5 16:57
     * @type [java.lang.String]
     * @return java.lang.Object
     */
    public String get(String key) {
        // Jedis jedis = jedisPool.getResource();
        String s = jedis.get(key);
        return s;
    }

    /**
     * @description 删除指定键
     * @author 30500
     * @date 2021/10/7 9:33
     * @type [java.lang.String 键]
     * @return boolean
     */
    public boolean del(String key) {
        Transaction multi = jedis.multi();
        try {
            // Jedis jedis = jedisPool.getResource();
            multi.del(key);
            multi.exec();
        } catch (Exception e) {
            e.printStackTrace();
            multi.discard();
            return false;
        }
        return true;
    }
}
